package com.oa.co.service.impl;
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */

import com.oa.co.common.R;
import com.oa.co.mapper.LeaveInformationMapper;
import com.oa.co.mapper.UserMapper;
import com.oa.co.pojo.Expense_account;
import com.oa.co.pojo.LeaveInformation;
import com.oa.co.pojo.Schedule;
import com.oa.co.service.LeaveInformationService;
import com.oa.co.service.ScheduleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author sunXuDong
 * @data 2023/5/4
 */

@Service("leaveInformationService")
public class LeaveInformationServiceImpl implements LeaveInformationService {
    @Autowired
    private LeaveInformationMapper leaveInformationMapper;
    @Autowired
    private ScheduleService scheduleService;

    @Override
    @Transactional
    public R add(LeaveInformation leaveInformation) {

        Integer add1 = leaveInformationMapper.add(leaveInformation);
        Schedule schedule = Schedule.getInstance();
        BeanUtils.copyProperties(leaveInformation,schedule);

        R add = scheduleService.add(schedule);

        if (add.getCode() == 200&& add1 == 1){

            return R.ok();
        }

        return R.error();
    }

    @Override
    @Transactional
    public R del(LeaveInformation leaveInformation) {
        if (leaveInformation!=null) {
            Schedule schedule = scheduleService.queryId(leaveInformation.getD_id());
            if (schedule.getD_code()==6){
                Integer del = scheduleService.del(leaveInformation.getD_id());
                Integer del1 = leaveInformationMapper.del(leaveInformation.getD_id());

                if (Objects.equals(del,del1)){
                    return R.ok().message("撤回成功");
                }

                return R.error();
            }else {
               return R.error().message("已在审批,撤回失败");
            }

        }

        return R.error().message("leaveInformation为空");
    }

    @Override
    public R query(String d_id) {
        List<LeaveInformation> list = leaveInformationMapper.query(d_id);

        return list !=null? R.ok().data("res",list):R.error();
    }
}
